package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.system.EntitySysRoleAd;
import com.loongtech.core.jpa.manager.ManagerBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("sysRoleAdManager")
public class SysRoleAdManager extends ManagerBase<EntitySysRoleAd>
{
	private static final long	serialVersionUID	= 9L;

    @Autowired
    private SysUserManager		    sysUserManager;

    /**
     * 根据userid获取授权的代理商名称集合
     * @return
     */
    public HashSet<String> getAdSetByUserId(int userId){
        HashSet<String> chanSet = new HashSet<>();

        //对超级管理员特殊处理
        if (sysUserManager.isAdmin(userId)) {
            chanSet.addAll(AdvConfigManager.AD_LIST);
            return chanSet;
        }

        List<String> list = findByQuery("select rc.ad from EntitySysRoleAd as rc, EntitySysUserRole as ur, EntitySysRole as sr where rc.roleId = sr.id and ur.roleId = sr.id and ur.userId=?", userId);
        if(null == list || list.isEmpty()){
            return chanSet;
        }

        for(String ad : list){
            if(null != ad) {
                chanSet.add(ad);
            }
        }
        return chanSet;
    }

	/**
	 * 获取一个角色当前已经被授权的代理商信息
	 * @param roleId
	 * @return
	 */
	public Set<String> getRoleAdSet(int roleId)
	{
		Set<String> set = new HashSet<>();
		List<EntitySysRoleAd> list = findBy(EntitySysRoleAd.K.roleId, roleId);
		if (null != list && !list.isEmpty())
		{
			for (EntitySysRoleAd channel : list)
			{
				set.add(channel.getAd());
			}
		}
		return set;
	}
	
	/**
	 * 获得角色当前归属的代理商列表
	 * @param roleId
	 * @return
	 */
	public List<String> getRoleAdList(int roleId)
	{
		List<String> list = new ArrayList<>();
		for (EntitySysRoleAd info : findBy(EntitySysRoleAd.K.roleId, roleId))
		{
			String channel = info.getAd();
			if (null != channel)
			{
				list.add(channel);
			}
		}
		return list;
	}
	
	/**
	 * 删除一条记录
	 * @param roleId
	 * @param channelId
	 * @return
	 */
	public boolean removeByRoleIdAndAd(int roleId, String channelId)
	{
		EntitySysRoleAd entity = getBy(EntitySysRoleAd.K.roleId, roleId, EntitySysRoleAd.K.ad, channelId);
		if (null != entity)
		{
			removeById(entity.getId());
			return true;
		}
		
		return false;
	}
	
	/**
	 * 插入一条记录
	 * @param roleId
	 * @return
	 */
	public boolean insertByRoleIdAndAd(int roleId, String channelId)
	{
		EntitySysRoleAd entity = getBy(EntitySysRoleAd.K.roleId, roleId, EntitySysRoleAd.K.ad, channelId);
		if (null == entity)
		{
			entity = new EntitySysRoleAd();
			entity.setRoleId(roleId);
			entity.setAd(channelId);
			persist(entity);
			return true;
		}
		
		return false;
	}

    /**
     * 判断一个代理商是否已经分配
     * @param channelId
     * @return
     */
	public boolean hasAdAssigned(String channelId){
        if(null == channelId || channelId.trim().isEmpty()){
            return false;
        }
        List<EntitySysRoleAd> list = findBy(EntitySysRoleAd.K.ad, channelId);
        return null !=list && list.size() > 0;
    }
}
